//
//  FinishViewController.m
//  work
//
//  Created by 徐宏飞 on 16/6/14.
//  Copyright © 2016年 方伟. All rights reserved.
//

#import "FinishViewController.h"
#import "MyReportModel.h"
#import "GoingProjectModel.h"
#import "WorkerCompleteCollectionViewCell.h"
#import "XHF_LbumViewController.h"  //相册



static NSString *collectionViewCell_ID = @"WorkerCompleteCollectionViewCell";


@interface FinishViewController ()<NetworkToolerDelegate, UICollectionViewDelegate,
UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UILabel *lblTime;
@property (weak, nonatomic) IBOutlet UILabel *lblAddress;
@property (weak, nonatomic) IBOutlet UILabel *lblowner;
@property (weak, nonatomic) IBOutlet UILabel *lblTel;
@property (weak, nonatomic) IBOutlet UILabel *lblPrice;

@property (weak, nonatomic) IBOutlet UILabel *lblWorkContent;



@property (weak, nonatomic) IBOutlet UICollectionView *upJobCollectionView;
@property (nonatomic ,strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) NSMutableArray *allImagesArray;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *lblContentH;

@property (nonatomic, strong) ZhuangxiuManager *manager;
@property (nonatomic, strong) NetworkTooler *network;

@end

@implementation FinishViewController


#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    self.title = _pickDay;
    self.allImagesArray = [NSMutableArray array];
    self.manager = [ZhuangxiuManager shareManager];
    _network  = [NetworkTooler new];
    
    
    [self configurationUpJobCollectionView];
    [self getTodayInfo];
    [self configurationData];
}






#pragma mark - Data
- (void) getTodayInfo{
    _network.delegate = self;
    
    NSDictionary *parameters = @{
                                 @"day": _pickDay,
                                 @"order_id":[NSString stringWithFormat:@"%@", _model.order_id],
                                 @"session_id":self.manager.sessionid
                                 };
    
    NSLog(@"上传参数:%@", parameters);
    
    [_network parameters:parameters withURL:getTaskByDay_show  withIsPaparameters:YES];
}

#pragma mark - NetworkToolerDelegate
- (void)returnData:(id)data{
    
    [GiabHUD hideUIBlockingIndicator];
    [self goingProjectTVendRefresh];
    self.bgView.hidden = YES;
    
    
    if (isNSDictionary) {
        
        MyReportModel *model = [MyReportModel modelWithDictionary:data];
       CGSize size = [NSString sizeWithText:model.task_content maxSize:CGSizeMake(SCREEN_WIDTH - 20, MAXFLOAT) font:FW_FONT(14)];
        self.lblContentH.constant = size.height;
        self.lblWorkContent.text = [NSString stringWithFormat:@"%@",model.task_content];
        
        NSArray *array = [model.piclist componentsSeparatedByString:@","];
        for (NSString *str in array) {
            if (![str isEqualToString:@""]) {
                [self.allImagesArray addObject:str];
            }
        }
        
        
        
        [self.upJobCollectionView reloadData];
        
    }
    
}

- (void)returnRequestError:(NSError *)error{
    
    [GiabHUD hideUIBlockingIndicator];
    [self goingProjectTVendRefresh];
    
}

- (void)returnMsg:(NSString *)Msg{
    [GiabHUD hideUIBlockingIndicator];
    [self goingProjectTVendRefresh];
    
    if ([Msg isEqualToString:@"你的网络有问题哦!"]) {
        self.bgView.hidden = NO;
        [self.allImagesArray removeAllObjects];
        [_upJobCollectionView reloadData];
        return;
    }
    [self showAlertWithTitle:@"温馨提示" message:Msg cancelButtonTitle:@"取消" otherButtonTitles:@"确定"];
}






#pragma mark - UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
//    return self.allImagesArray.count;
    return self.allImagesArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    WorkerCompleteCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionViewCell_ID forIndexPath:indexPath];

    NSString *str = [self.allImagesArray[indexPath.row] stringByAppendingString:avatarcase];
    NSURL *url = [NSURL URLWithString:str];
    [cell.imgComplete sd_setImageWithURL:url placeholderImage:IMAGEPLACEHOLDER];
    cell.deleteBtn.hidden = YES;

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    //大图浏览
    if (self.allImagesArray.count == 0 || self.allImagesArray.count < indexPath.row) {
        return;
    } else {
        //进入相册
        XHF_LbumViewController *lbumVC = [[XHF_LbumViewController alloc] init];
        lbumVC.imgArr = self.allImagesArray;
        //        NSLog(@"AA%@", self.imgCarouseList);
        lbumVC.currentIndex = indexPath.row;
        lbumVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:lbumVC animated:YES completion:nil];
    }
    
    
}






#pragma mark - CustomDelegate


#pragma mark - event response



#pragma mark - private methods
-(void)goingProjectTVendRefresh{
    [_upJobCollectionView.mj_header endRefreshing];
    [_upJobCollectionView.mj_footer endRefreshing];
}



- (void)configurationUpJobCollectionView{
    
    //item 比例4:5
    CGSize size = CGSizeMake(118, 100);
    //4  4s  5  5s
    if (SCREEN_WIDTH == 320) {
        size = CGSizeMake(86, 70);
    }
    
    //6 6s
    if (SCREEN_WIDTH == 375) {
        size = CGSizeMake(105, 90);
    }
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.itemSize = size;
    self.flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 20, 10);
    self.flowLayout.minimumLineSpacing = 12;
    self.flowLayout.minimumInteritemSpacing = 8;
    
    _upJobCollectionView.backgroundColor = UIColorFromRGB(0xEBEBEB);
    _upJobCollectionView.collectionViewLayout = self.flowLayout;
    _upJobCollectionView.delegate = self;
    _upJobCollectionView.dataSource =self;
    [_upJobCollectionView registerNib:[UINib nibWithNibName:@"WorkerCompleteCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:collectionViewCell_ID];
    
}

- (void)configurationData{
    
    NSString *start = [NSString getBeijingDayWithStrTime:self.model.start_day];
    NSString *end = [NSString getBeijingDayWithStrTime:self.model.end_day];
    self.lblTime.text = [NSString stringWithFormat:@"时间: %@至%@", start, end];
    self.lblAddress.text = [NSString stringWithFormat:@"工作地点: %@", self.model.address];
    self.lblowner.text = [NSString stringWithFormat:@"业主: %@", self.model.contact_name];
    self.lblTel.text = [NSString stringWithFormat:@"联系电话: %@", self.model.mobile];
    self.lblPrice.text = [NSString stringWithFormat:@"价格: %@元/平方", self.model.price];
}


#pragma mark - gettsers and setters




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
